define(['template/bole/indexTpl','wx','common','vendor/bootstrap-slider.min','vendor/jquery.dataTables','wx.ajax','wx.tpl','wx.validator','wx.upload','vendor/bootstrap-typeahead'],function(tpl,wx,common){
    var bole = {};
    var allDeparts;
    var level = [
        '员工级（技术除外）',
        '主管、初级开发',
        '经理、中高级开发、产品',
        '初级  开发',
        '中级  开发',
        '高级经理、总监',
        'VP 及以上'
    ];
    function init(id){
        var t = new Date().getTime();
        $.when(
            $.ajax('/web/res/css/layout.css?_='+t),
            $.ajax('/web/res/css/bole.css?_='+t),
            $.ajax('/department/getall',{dataType:'json'}),
            $.ajax('/web/res/css/bootstrap-slider.min.css'),
            $.ajax('/web/res/css/jquery.dataTables.min.css')
            ).done(function(layout,bole,dep,sliderCss,tableCss){
                allDeparts = dep;
                $('<style type="text/css">').append(layout,bole,sliderCss,tableCss).appendTo('head');
                $('#window-frame-bole').html(tpl.container);
                $('#Js-bole-cate').delegate('li','click',function(){
                    $(this).addClass('active').siblings('.active').removeClass('active');
                    var index = $(this).data('id');
                    if(index == 'user'){
                        $('#Js-bole-body').html(wx.tpl(tpl.search,{'dep':dep[0].data}),tpl.user);
                        userAction();
                    } else if(index == 'jobs'){
                        jobsAction();
                    } else if(index == 'recom'){
                        recomAction();
                    }
                });
                $('#Js-bole-cate').find('[data-id="user"]').trigger('click');
        });
	}

    function userAction(){
        $(".Js-slider").slider({tooltip:'hide'});
        $(".Js-slider").on("slide", function(slideEvt) {
            $(this).next('span').html(slideEvt.value[0]+' - '+slideEvt.value[1]);
        });
        $('#Js-showAdv').click(function(){
            $(this).toggleClass('up');
            $('#Js-advancedSearch').slideToggle();
        });
        $('.dropdown-toggle').dropdown();
        $('.dropdown-menu').delegate('li','click',function(){
            $('#Js-dep').text($(this).find('a').text());
        });
        $('#Js-search').click(function(){
            var dep = $('#Js-dep').text(),
                integral = $('#Js-jifen').text(),
                recom = $('#Js-recom').text();
            dep = dep == '全部'?'1':dep;
            wx.sendData('/bole/user/search-all','dep='+dep+'&integral='+integral+'&recom='+recom).success(function(data){
                showUserTable(data.result);
            }).fail(function(){
                $('#Js-searchResult_wrapper').remove();
            });

        });
        $('#Js-who-search').typeahead({
             items:10,
             ajax: {
                 url:'/who/search',
                 triggerLength: 1
             },
             grepper:function(data){
                 return data;
             },
             itemSelected:function(item,id){
                wx.sendData('/bole/user/search-one','id='+id+'&code='+item.data('code')).success(function(data){
                    showUserTable(data.result);
                }).fail(function(){
                    $('#Js-searchResult_wrapper').remove();
                });
            }
        });
    }

    function showUserTable(data){
        $('#Js-searchResult_wrapper').remove();
        $('#Js-bole-body').append(tpl.table);
        $('#Js-searchResult').DataTable({
            searching: false,
            aoColumnDefs: [
             { "bSortable": false, "aTargets": [ 0,1,2] ,"sWidth": "auto"}
            ],
            data:data,
            columns: [
                { data: 'code' },
                { data: 'name' },
                { data: 'department' },
                { data: 'recommend' },
                { data: 'integral' }
            ],
            aaSorting: [[ 3, "desc" ]],
            language:common.dataTableText
        });
        $('#Js-searchResult tbody').delegate('tr', 'click', function(event) {
        });
    }

    function jobsAction(){
        $('#Js-bole-body').empty();
        wx.sendData('/bole/jobs/get-all').success(function(data){
            for(var i=0;i<data.result.length;i++){
                if(data.result[i].state == 1){
                    data.result[i].state = '<a class="action" href="javascript:;" data-id="'+data.result[i].id+'">显示</a>';
                } else {
                    data.result[i].state = '<a class="action" href="javascript:;" data-id="'+data.result[i].id+'">隐藏</a>';
                }
            }
            $('#Js-bole-body').append(tpl.jobsTable);
            $('#Js-jobsTable').DataTable({
                data:data.result,
                columns: [
                    { data: 'department' },
                    { data: 'title' },
                    { data: 'level' },
                    { data: 'create_time' },
                    { data: 'state' },
                ],
                aaSorting: [[ 3, "desc" ]],
                language:common.dataTableText
            });
            $('#Js-jobsTable').delegate('.action', 'click', function(event) {
                var $this = $(this);
                var id = $this.data('id');
                var state = $this.text() == '显示'?0:1;
                wx.sendData('/bole/jobs/change','id='+id+'&state='+state).success(function(data){
                    $this.text(state == 0? '隐藏':'显示');
                });
            });
            $('#Js-jobsTable_length').append(tpl.jobsAdd);
            $('#Js-jobs-add').click(jobsAdd);
        }).fail(function(){
            $('#Js-bole-body').append(tpl.jobsTable);
            $('#Js-jobsTable').DataTable({language:common.dataTableText});
            $('#Js-jobsTable_length').append(tpl.jobsAdd);
            $('#Js-jobs-add').click(jobsAdd);
        });
    }

    function jobsAdd(){
            $('#Js-bole-body').html(wx.tpl(tpl.jobsAddContent,{'level':level,'dep':allDeparts[0].data}));
            wx.validator();
            $('.dropdown-toggle').dropdown();
            $('#Js-depMenu').delegate('li','click',function(){
                $('#Js-dep').text($(this).find('a').text());
            });
            $('#Js-titleMenu').delegate('li','click',function(){
                $('#Js-title').text($(this).find('a').text());
            });
            $('#Js-jobs-addBack').click(function(){
                $('#Js-bole-cate').find('[data-id="jobs"]').trigger('click');
            });
            $('form[name="jobsForm"]').on('validator:before',function(){
                $(this).find('input[name="dep"]').val($('#Js-dep').text());
                $(this).find('input[name="level"]').val($('#Js-level').text());
                return true;
            });
            $('form[name="jobsForm"]').on('validator',function(event,data){
                if(data.status){
                    $.succ();
                    $('#Js-bole-cate').find('[data-id="jobs"]').trigger('click');
                }
            });
    }

    function recomAction(){
        $('#Js-bole-body').empty();
        wx.sendData('/bole/recommend/get-all').success(function(data){
            $('#Js-bole-body').append(tpl.recommendTable);
            $('#Js-recommendTable_length').append(tpl.recommendAdd);
            $('#Js-recommendTable').DataTable({
                data:data.result,
                columns: [
                    { data: 'department' },
                    { data: 'title' },
                    { data: 'level' },
                    { data: 'create_time' },
                    { data: 'state' },
                ],
                aaSorting: [[ 3, "desc" ]],
                language:common.dataTableText
            });
            $('#Js-recommend-add').click(recommendAdd);
        }).fail(function(){
            $('#Js-bole-body').append(tpl.recommendTable);
            $('#Js-recommendTable').DataTable({language:common.dataTableText});
            $('#Js-recommendTable_length').append(tpl.recommendAdd);
            $('#Js-recommend-add').click(recommendAdd);
        });
    }

    function recommendAdd(){
        $('#Js-bole-body').html(wx.tpl(tpl.recommendAddContent,{'level':level,'dep':allDeparts[0].data}));
        $('.dropdown-toggle').dropdown();
        wx.upload($('#Js-upload'));
        $('input[type="file"]').on('upload',function(event,data){
            if(data.status){
                $('#Js-uploadName').text(data.name).attr('href',data.path);
                $.succ('上传成功');
            }
        });
        $('#Js-resume-submit').click(function(){

        });
        $('#Js-depMenu').delegate('li','click',function(){
            $('#Js-dep').text($(this).find('a').text());
        });
        $('#Js-titleMenu').delegate('li','click',function(){
            $('#Js-level').text($(this).find('a').text());
        });
        $('#Js-bole-recommSearch').typeahead({
             items:10,
             ajax: {
                 url:'/who/search',
                 triggerLength: 1
             },
             grepper:function(data){
                 return data;
             },
             itemSelected:function(item,id){
                wx.sendData('/who/detail','id='+id).success(function(data){
                    $('#Js-bole-code').val(data.data.code);
                    $('#Js-bole-depart').val(data.data.department);
                    $('#Js-bole-level').val(data.data.level);
                }).fail(function(){
                });
            }
        });
    }
    bole.init = init;
    return bole;
});